package com.example.tijianbackend.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.tijianbackend.pojo.Users;
import com.example.tijianbackend.result.Result;
import com.example.tijianbackend.service.UsersService;
import com.example.tijianbackend.utils.ObjectUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author: bong~
 * @description: TODO
 * @date: 2024/6/13 16:03
 * @version: 1.0
 */

@RestController
@Slf4j
@RequestMapping("/users")
@Api(tags ="users")
@CrossOrigin("*")
public class UserController {


    @Autowired
    private UsersService usersService;

    @PostMapping("/login")
    @ApiOperation(value = "登录接口",
            notes = "  ○ 参数：Users对象\n" +
                    "  ○ 返回值：Users对象\n" +
                    "  ○ 功能：根据手机号码和密码进行登录验证")
    public Result login(@RequestBody Users users){

        String userId=users.getUserId();
        String password=users.getPassword();

        System.out.println(users);

        if(userId==null || password==null){
            return Result.error("账号或密码为空");
        }

        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("userId",userId);

        Users user=usersService.getOne(queryWrapper);

        if(user==null){
            return Result.error("改手机号未注册");
        }else{
            queryWrapper.eq("password",password);
            user=usersService.getOne(queryWrapper);

            if(user==null){
                return Result.error("密码错误");
            }
        }
        System.out.println(user);
        return Result.success("登录成功",user);
    }

    @PostMapping("/register")
    @ApiOperation(value = "注册接口")
    public Result register(@RequestBody Users users){

        users.setUserType(1);
        if(!ObjectUtils.AllFieldNotNullOrEmpty(users)) {
            return Result.error("注册失败，信息填写不全");
        }else{
            String userId=users.getUserId();

            QueryWrapper queryWrapper=new QueryWrapper();
            queryWrapper.eq("userId",userId);

            Users user=usersService.getOne(queryWrapper);
            if(user!=null){
                return Result.error("改手机号已被注册");
            }
        }
        usersService.save(users);
        return Result.success("注册成功",users);

    }
}
